package rmi;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class Client implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String RMI_PORT_SERVER ="5555/server";
	private static final String ERROR_CONNECTION= "java.net.ConnectException: Connection refused";
	
	private Remote remoteObject;
	
	public Remote getRemoteObject() {
		return remoteObject;
	}

	public Client(String ip) throws MalformedURLException, RemoteException, NotBoundException {
		
		try{
			remoteObject=Naming.lookup("rmi://" + ip + RMI_PORT_SERVER);
		}catch(Exception e){
			if (e.getCause().toString().equals(ERROR_CONNECTION)){
				FacesContext ctx = FacesContext.getCurrentInstance();  
			ctx.addMessage(null, new FacesMessage("Erro, RMI Offline")); 
			}
			
		}
	}
	
}
